package channel;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.net.SocketTimeoutException;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBElement;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;

import model.request.configRequest.ConfigResolveClasses;
import model.request.configRequest.ObjectFactory;

public class UCSAPIService
{
	private String outCookie = null;
	protected String hostname;
	protected String ipAddr;
	protected String username;
	protected String passwd;
	protected int port;
	protected boolean isSecure;
	protected comChannel commChannel;
	 
	public UCSAPIService(String hostname, String username,String passwd, int port, boolean isSecure) throws SocketTimeoutException, Exception 
	{
		this.hostname = hostname;
		this.username = username;
		this.passwd = passwd;
		this.port = port;
		this.isSecure = isSecure;
			  
		this.commChannel = new comChannel(this.hostname == null ? this.ipAddr : this.hostname, this.port, this.isSecure);
		System.setProperty("neprobe.inventory.log", "on");
		try
		{
			toLoginRequest();
		}
		catch(SocketTimeoutException e)
		{
			throw e;
		}
		catch(Exception e)
		{
			throw e;
		}   
	}
	
	public void toLoginRequest() throws Exception
	{
		
		model.request.AaaLogin loginRequest = new model.request.AaaLogin();
		loginRequest.setInName(username);
		loginRequest.setInPassword(passwd);
		JAXBContext jcontext;
		try
		{
			jcontext =	JAXBContext.newInstance(model.request.AaaLogin.class);
			Marshaller jaxbmarshal=jcontext.createMarshaller();
			jaxbmarshal.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
			ByteArrayOutputStream buffer=new ByteArrayOutputStream();
			jaxbmarshal.marshal(loginRequest,buffer);
			UCSResponse res = commChannel.getCommChannel(new String(buffer.toByteArray()));
			System.out.println(res);
			
			ByteArrayInputStream input=new ByteArrayInputStream(res.getBuff().toString().getBytes());
			
			JAXBContext jresponse=JAXBContext.newInstance(model.response.AaaLogin.class);
			Unmarshaller unmar=jresponse.createUnmarshaller();
			model.response.AaaLogin loginresponse=(model.response.AaaLogin)unmar.unmarshal(input);
			
			outCookie=loginresponse.getOutCookie();
			
			System.out.println(outCookie);
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
		
	}
	
	public JAXBElement<model.response.configResponse.ConfigResolveClasses> toConfigResolve() throws Exception
	{
		
		ObjectFactory obj= new ObjectFactory();
		ConfigResolveClasses conf=obj.createConfigResolveClasses();
		conf.setCookie(this.outCookie);
		conf.setInHierarchical("true");
		model.request.ClassIdRequest.ObjectFactory objRequest=new model.request.ClassIdRequest.ObjectFactory();
		model.request.ClassIdRequest.ClassIdSet classIDRequest=objRequest.createClassIdSet();
		
		classIDRequest.getClassId().add(new model.request.ClassIdRequest.ClassIdSet.ClassId("equipmentChassis"));
		classIDRequest.getClassId().add(new model.request.ClassIdRequest.ClassIdSet.ClassId("equipmentFanModule"));
		classIDRequest.getClassId().add(new model.request.ClassIdRequest.ClassIdSet.ClassId("computeBlade"));
		classIDRequest.getClassId().add(new model.request.ClassIdRequest.ClassIdSet.ClassId("equipmentPsu"));
		classIDRequest.getClassId().add(new model.request.ClassIdRequest.ClassIdSet.ClassId("networkElement"));
		classIDRequest.getClassId().add(new model.request.ClassIdRequest.ClassIdSet.ClassId("lsServer"));
		
		
		JAXBElement<model.request.ClassIdRequest.ClassIdSet> object=obj.createConfigResolveClassesInIds(classIDRequest);
		
		conf.getContent().add(object);
		JAXBContext jc;
		jc=JAXBContext.newInstance(ConfigResolveClasses.class);
		Marshaller jaxbmarshal=jc.createMarshaller();
		jaxbmarshal.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
		ByteArrayOutputStream buff=new ByteArrayOutputStream();
		
		//ConfigResolveClasses confi=new ConfigResolveClasses();
		jaxbmarshal.marshal(conf,buff);
		UCSResponse res = commChannel.getCommChannel(new String(buff.toByteArray()));
		String xmlBuff = res.getBuff().toString();
		xmlBuff = xmlBuff.replaceAll("\"<", "\"");
		xmlBuff = xmlBuff.replaceAll(">\"", "\"");
		SaveInXML.saveAsXML("ucsxml", xmlBuff.getBytes());
		ByteArrayInputStream input=new ByteArrayInputStream(xmlBuff.getBytes());
		JAXBContext jresponse=JAXBContext.newInstance(model.response.configResponse.ConfigResolveClasses.class);
		Unmarshaller unmar=jresponse.createUnmarshaller();
		@SuppressWarnings("unchecked")
		JAXBElement<model.response.configResponse.ConfigResolveClasses> confresponse=(JAXBElement<model.response.configResponse.ConfigResolveClasses>)unmar.unmarshal(input);
		
		return confresponse;
			
	}
	
}
